﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using System.Net.Sockets;
using System.Windows.Media.Effects;
using System.Media;

namespace FCS.LMOC
{
    /// <summary>
    /// A simple class to hold the information about a valve
    /// </summary>
    class Valve
    {
        private static Brush _valveOnBrush;
        private static Brush _valveOffBrush;
        private static Brush _valveDisabledBrush;

        private bool _state;
        private bool _disabled;
        
        public readonly string Name;
        public int X { get; private set; }
        public int Y { get; private set; }
        public Ellipse Dot { get; set; }

        /// <summary>
        /// The size of a valve's dot.
        /// </summary>
        public const int DotSize = 25;

        static Valve()
        {
            _valveOnBrush = new SolidColorBrush(Color.FromRgb(40, 180, 40));
            _valveOffBrush = new SolidColorBrush(Colors.Black);
            _valveDisabledBrush = new SolidColorBrush(Colors.Crimson);
        }

        /// <summary>
        /// Gets or sets the state of the valve
        /// </summary>
        public bool State
        {
            get
            {
                return _state;
            }

            set
            {
                if (!_disabled)
                {
                    _state = value;

                    if (value)
                        Dot.Fill = _valveOnBrush;
                    else
                        Dot.Fill = _valveOffBrush;
                }
            }
        }

        public bool Disabled
        {
            get
            {
                return _disabled;
            }

            set
            {
                _disabled = value;
                if (_disabled)
                {
                    Dot.Fill = _valveDisabledBrush;
                    Dot.Stroke = _valveDisabledBrush;
                }
                else
                {
                    if (_state)
                        Dot.Fill = _valveOnBrush;
                    else
                        Dot.Fill = _valveOffBrush;
                    
                    Dot.Stroke = _valveOnBrush;
                }
            }
        }

        public Valve(string name, int x, int y)
        {
            X = x;
            Y = y;
            Name = name;

            Dot = new Ellipse();
            Dot.Width = DotSize;
            Dot.Height = DotSize;
            Dot.Opacity = .8;

            Dot.Stroke = _valveOnBrush;
            Dot.StrokeThickness = 1.5;

            DropShadowEffect dropShadow = new DropShadowEffect();
            dropShadow.BlurRadius = 10;
            dropShadow.Opacity = .8;
            dropShadow.ShadowDepth = 3;
            Dot.Effect = dropShadow;

            State = false;
        }

        /// <summary>
        /// Returns the distance between the given point and the valve dot.
        /// </summary>
        public double GetDistanceFromPointSquared(Point p)
        {
            return Math.Pow(X - p.X, 2) + Math.Pow(Y - p.Y, 2);
        }
    }
}
